package com.example.controller;


import com.example.entity.User;
import com.example.mapper.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class UserController {
    @Autowired
    private UserRepository userRepository;

    @GetMapping("/greeting")
    public String greetingForm(Model model) {
        model.addAttribute("user", new User());
        return "greeting";
    }

    @PostMapping("/greeting")
    public String greetingSubmit(@ModelAttribute User user) {

        User newUser = new User();

//        newUser.setReportNumber(user.getReportNumber());
        newUser.setTestType(user.getTestType());
        newUser.setApplyDate(user.getApplyDate());
        newUser.setNameOfEntrustingParty(user.getNameOfEntrustingParty());
        newUser.setContacts(user.getContacts());
        newUser.setTelephone(user.getTelephone());
        newUser.setMailbox(user.getMailbox());
        newUser.setSystemName(user.getSystemName());
        newUser.setVersionNumber(user.getVersionNumber());
        newUser.setDevelopmentTool(user.getDevelopmentTool());
        newUser.setSystemCharacteristics(user.getSystemCharacteristics());
        newUser.setMainFunctionalModules(user.getMainFunctionalModules());

        userRepository.save(user);

        return "result";

    }

    @GetMapping("/all")
    public String getMessage(Model model) {

        Iterable<User> users = userRepository.findAll();

        model.addAttribute("users", users);
        return "all";
    }


}